package com.briup.jd2407cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.briup.jd2407cms.domain.Article;
import com.briup.jd2407cms.domain.Category;
import com.briup.jd2407cms.domain.extend.CategoryExtend;
import com.briup.jd2407cms.exception.ServiceException;
import com.briup.jd2407cms.mapper.ArticleMapper;
import com.briup.jd2407cms.result.ResultCode;
import com.briup.jd2407cms.service.CategoryService;
import com.briup.jd2407cms.mapper.CategoryMapper;
import com.briup.jd2407cms.utils.CategoryListener;
import com.briup.jd2407cms.utils.CategoryParentIdConverter;
import com.briup.jd2407cms.utils.ExcelUtils;
import com.sun.corba.se.spi.orbutil.fsm.Guard;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* @author 王杰_
* @description 针对表【cms_category】的数据库操作Service实现
* @createDate 2024-11-21 10:10:31
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{

    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    ArticleMapper articleMapper;
    @Autowired
    ExcelUtils excelUtils;
    @Override
    public IPage<Category> queryByPageAndCondition(Integer pageNum, Integer pageSize, Integer parentId) {
        Page<Category> Page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(parentId!=null,Category::getParentId,parentId);
        lqw.orderByAsc(Category::getParentId).orderByAsc(Category::getOrderNum);
        IPage<Category> page = categoryMapper.selectPage(Page, lqw);
        return page;
    }

    @Override
    public List<CategoryExtend> queryAllParent() {
        List<CategoryExtend> categoryExtends = categoryMapper.selectAllParent();
        return categoryExtends;
    }

    @Override
    public void add(Category category) {
        if (!StringUtils.hasText(category.getName())){
            throw new ServiceException(ResultCode.DATA_IS_EMPTY);
        }
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Category::getName,category.getName());
        Category category1 = categoryMapper.selectOne(lqw);
        if (category1!=null){
            log.error("新增栏目时，栏目名称重复");
            throw new ServiceException(ResultCode.DATA_NOT_UNIQUE);
        }
        Integer parentId = category.getParentId();
        if (parentId!=null&&categoryMapper.selectById(parentId)==null){
            throw new ServiceException(ResultCode.PARAM_IS_INVALID);
        }
        Integer order_num= null;

        if (categoryMapper.selectCount(null)!=0){
            Integer maxOrderNum = categoryMapper.getMaxOrderNum();
            if (maxOrderNum==10){
                order_num=10;
            }else if (maxOrderNum<10){
                order_num = maxOrderNum+1;
            }
        }else {
            if (category.getOrderNum()!=null){
                order_num=category.getOrderNum();
            }else {
                order_num = 1;
            }
        }

        category.setOrderNum(order_num);
        categoryMapper.insert(category);
    }

    @Override
    public List<Category> queryAllOneLevel() {
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.isNull(Category::getParentId);
        List<Category> categories = categoryMapper.selectList(lqw);
        if (categories==null|| categories.isEmpty()){
            throw new ServiceException(ResultCode.CATEGORY_NOT_EXIST);
        }
        return categories;
    }

    @Override
    public Category queryById(Integer id) {
        Category category = categoryMapper.selectById(id);
        return category;
    }

    @Override
    public void edit(Category category) {
        String name = category.getName();
        Integer id = category.getId();
        Category oldCategory = categoryMapper.selectById(id);
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Category::getName,name);
        Category category1 = categoryMapper.selectOne(lqw);
        if (category1!=null){
           category.setName(null);
        }
        Integer parentId = category.getParentId();
        if (oldCategory.getParentId()==null&&parentId!=null){
            throw new ServiceException(ResultCode.CATEGORY_LEVEL_SETTING_ERROR);
        }
        if (oldCategory.getParentId()!=null&&parentId!=null){
            Category category2 = categoryMapper.selectById(parentId);
            if (category2==null){
                throw new ServiceException(ResultCode.PARAM_IS_INVALID);
            }
        }
        categoryMapper.updateById(category);
    }

    @Override
    public void deleteById(Integer id) {
        Category category = categoryMapper.selectById(id);
        if (category.getParentId()==null){
            LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Category::getParentId,id);
            if (categoryMapper.selectCount(lqw)>0){
                throw new ServiceException(ResultCode.PARAM_IS_INVALID);
            }else {
                Integer count = articleMapper.getArticleNumByCategoryId(id);
                if (count>0){
                    throw new ServiceException(ResultCode.PARAM_IS_INVALID);
                }
            }
        }
        categoryMapper.deleteById(id);
    }

    @Override
    public void deleteByIdAll(List<Integer> ids) {
        for (Integer i:ids){
            deleteById(i);
        }
    }

    @Override
    public void InsertInBatch(MultipartFile file) {
        CategoryParentIdConverter.list=queryAllOneLevel();
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        List<Category> list = excelUtils.importData(file, Category.class, new CategoryListener(),null);
        for (Category category:list){
            lqw.eq(Category::getName,category.getName());
            Category category1 = categoryMapper.selectOne(lqw);
            if (category1!=null){
                lqw.clear();
                continue;
            }
            lqw.clear();
            categoryMapper.insert(category);
        }
    }

    @Override
    public List<Category> queryAll() {
        return categoryMapper.selectList(null);
    }

    @Override
    public void export(HttpServletResponse response) {
        CategoryParentIdConverter.list = queryAllOneLevel();
        List<Category> categories = queryAll();
        excelUtils.exportExcel(response,categories,Category.class,"栏目信息表");
    }


}




